const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    info: null,
    coupon_list: [],
    goods_list: [],
    tabIndex:0,
    navigator: { hide: 0 },
    src: [],
    is_myshop: false,
    fenxiangzhuan: {
      show: false,
      width: '86rpx;',
      height: '88rpx;',
      bottom: '380rpx;',
      top: '10rpx'
    }
  },
  jump:function(){
    wx.navigateTo({
      url: '/pages/shop/tccar/tccar',
    })
  },
  submitInfo: function (e) {
    var t = this;
    var shop_id = e.detail.target.dataset.shop_id;
    var formid = e.detail.formId;
    app.ihttpPost("https://www.58zltc.com/api.php/Index/addFormId", { form_id: formid, openid: app.globalData.openid }, function (res) { })
    wx.navigateTo({
      url: '/pages/shop/yuyue/yuyue?shop_id='+shop_id,
    })
  },
  //快捷导航
  navigator: function (e) {
    var width = this.data.navigator.width
    if (this.data.navigator.hide == 1) {
      var offset = '0'
      var mum = 0;
    } else {
      var offset = width * 0.64
      var mum = 1;
    }
    this.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = this.animation.export();
    v.hide = mum;
    this.setData({
      //输出动画
      navigator: v
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    wx.showLoading({
      title: '加载中…', 
    })
    
    let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
    this.setData({
      token: extConfig.appid,
      shop_id: options.shop_id
    })
    var scene = decodeURIComponent(options.scene);
    if (scene != 'undefined') {
      var shop_id = scene;
    } else {
      var shop_id = options.shop_id;
    }
    console.log(app.globalData)
    let that = this; 

    //快速导航
    app.ihttpGet("https://www.58zltc.com/api.php/Index/getNav", function (res) {
      var v = that.data.navigator;
      v.info = res.data
      that.setData({
        navigator: v
      })
    })
    //屏幕宽度
    wx.getSystemInfo({
      success: function (res) {
        var width = res.screenWidth;
        var v = that.data.navigator;
        v.width = width
        that.setData({
          navigator: v
        })
      },
    })

    //实例化一个动画
    this.animation = wx.createAnimation({
      // 动画持续时间，单位ms，默认值 400
      duration: 300,
      /**
       * linear 动画一直较为均匀
       * ease  从匀速到加速在到匀速
       * ease-in 缓慢到匀速
       * ease-in-out 从缓慢到匀速再到缓慢
       * step-start 动画一开始就跳到 100% 直到动画持续时间结束 一闪而过
       * step-end  保持 0% 的样式直到动画持续时间结束    一闪而过
       */
      timingFunction: 'ease',
      // 延迟多长时间开始
      delay: 100,
      /**
       * 以什么为基点做动画 效果自己演示
       * left,center right是水平方向取值，对应的百分值为left=0%;center=50%;right=100%
       * top center bottom是垂直方向的取值，其中top=0%;center=50%;bottom=100%
       */
      transformOrigin: 'right',
      success: function (res) {

      }
    })
    var width = this.data.navigator.width;
    var offset = width * 0.64;
    that.animation.translateX(offset).step()
    var v = this.data.navigator
    v.animation = that.animation.export()
    v.hide = 1
    that.setData({
      //输出动画
      navigator: v
    })
    var url = 'https://www.58zltc.com/api.php/Shop/item/shop_id/' + shop_id + '/openid/' + app.globalData.openid;
    app.ihttpGet(url,function(res){
      console.log(res)
      if (res.data.openid == app.globalData.openid) {
        that.setData({
          is_myshop: true
        })  
      }
      that.setData({
        info: res.data
      })
    })
    
    wx.request({
      url: 'https://www.58zltc.com/api.php/Goods/allHouDong?shop_id=' + shop_id + '&token=' + this.data.token,
      success: res => {
        this.setData({
          goods_list: res.data
        })
      }
    })
    
    wx.request({
      url: 'https://www.58zltc.com/api.php/Coupon?shop_id=' + shop_id + '&token=' + this.data.token,
      success: res => {
        this.setData({
          coupon_list: res.data
        })
        wx.hideLoading()
      }
    })
  },
  onReady: function () {
    app.confirmCommission()
  },
  longtap: function (e) {
    console.log(111);
    var that = this;
    var url = e.currentTarget.dataset.src;
    console.log(url);
    this.data.src.push(url);
    wx.previewImage({
      urls: that.data.src
    })
  },
  follow: function (e) {
    var info = this.data.info;
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.shop_id = info.shop_id;
    app.ihttpPost('https://www.58zltc.com/api.php/Shop/follow', data, function (res) {
      if (res.data.err_code === 0) {
        if (res.data.err_msg == '-1') {
          info.is_follow = false;
        } else {
          info.is_follow = true;
        }
        that.setData({
          info: info
        })
        wx.hideLoading()
      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },
  bindCall: function (e) {
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },
  openLocation: function(e){
    var data = e.currentTarget.dataset;
    wx.openLocation({
      latitude: Number(data.latitude),
      longitude: Number(data.longitude),
      name: data.name,
      address: data.address
    })
  },
  setTab:function(e){
    this.setData({
      tabIndex: e.currentTarget.dataset.id
    })
  },
  getCoupon: function(e){
    let that = this;
    const id = e.currentTarget.dataset.id;
    var post_data = {};
    post_data.coupon_id = id;
    post_data.openid = app.globalData.openid;
    app.ihttpPost("https://www.58zltc.com/api.php/Coupon/getCoupon", post_data, function(res){
      app.iModel(res.data.err_msg);
    })
  },
  renling: function(e){
    wx.navigateTo({
      url: 'renling?shop_id='+this.data.info.shop_id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this;
    const shop = this.data.info;
    return {
      title: shop.name,
      path: '/pages/shop/detail?shop_id=' + shop.shop_id + '&fenxiang=' + app.globalData.openid + '&fWid=' + app.globalData.wid,
      success: function (res) {
        var a = that.data.fenxiangzhuan
        a.show = false
        that.setData({
          fenxiangzhuan: a
        })
        // 转发成功
        wx.showToast({
          title: '转发成功！',
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  //小程序码
  qrcode:function(e){
    wx.showLoading({
      title: '拼命生成中',
    })
    var that = this
    wx.request({
      url: 'https://www.58zltc.com/api.php/Qrcode/getShopWxaCode/shop_id/' + that.data.info.shop_id +'/token/'+that.data.token,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        var url = res.data.data.url;
        var urls = [];
        urls.push(url);
        wx.previewImage({
          urls: urls
        })
      },
      complete:function(e){
        wx.hideLoading();
      }
    })
   
  },
  previewImg: function (e) {
    var url = e.target.dataset.url;
    var urls = e.target.dataset.urls;
    console.log(e)
    wx.previewImage({
      current: url,
      urls: urls
    })
  },
  copyTBL: function (e) {
    var that = this;
    var pages = getCurrentPages();
    var currentPage = pages[pages.length - 1]; 
    var url = currentPage.route;
    var options = currentPage.options; 
    wx.setClipboardData({
      data: '/' + url + '?shop_id=' + options.shop_id,
      success: function (res) {
        // self.setData({copyTip:true}), 
        wx.showToast({
          title: '复制成功',
        }) 
      }
    });
  },
  previewImage: function (e) {
    let that = this;
    var img_url = e.currentTarget.dataset.url;
    var reqData = '';
    // wx.request({
    //   url: img_url,
    //   success: function (res) {
    //     reqData = res.data;
    //     wx.previewImage({
    //       current: reqData,
    //       urls: [reqData]
    //     })
    //   },
    //   fail: function () {
    //     wx.previewImage({
    //       current: reqData,
    //       urls: [reqData]
    //     })
    //   }
    // })
    wx.previewImage({
      current: img_url,
      urls: [img_url]
    })
  },
  showPinImg: function (e) {
    var url = e.currentTarget.dataset.url;
    var urls = e.currentTarget.dataset.urls;
    wx.previewImage({
      current: url,
      urls: urls
    })
  },
  changeFXZ: function () {
    var that = this;
    var a = that.data.fenxiangzhuan
    a.show ? a.show = false : a.show = true
    that.setData({
      fenxiangzhuan: a
    })
  },
  jumpWeb: function () {
    var that = this
    if (that.data.info.web_link) {
      wx.navigateTo({
        url: '/pages/index/web/web?url=' + that.data.info.web_link,
      })
    }
  },
  have_VIP: function (e) {
    console.log(e.currentTarget.dataset.shop_id)
    let that = this
    var shop_id = e.currentTarget.dataset.shop_id
    var my_openid = app.globalData.openid
    app.ihttpGet("https://www.58zltc.com/api.php/MyCard/index/openid/" + my_openid + '/shop_id/' + shop_id, function (res) {
      var data = res.data
      //有会员卡直接跳转
      if (data.mine[0]) {
        wx.navigateTo({
          url: '/pages/mine/shop/mycard/mycarddetail?id=' + data.mine[0].id,
        })
      }
      //没有会员卡提示开通
      else {
        wx.showModal({
          title: '提示',
          content: '请先开通会员',
          success: function (resq) {
            if (resq.confirm) {
              console.log('用户点击确定')
              //店铺是否添加会员卡
              app.ihttpGet("https://www.58zltc.com/api.php/MyCard/haveShopVipCard/shop_id/" + shop_id, function (res_1) {
                if (res_1.data != 0) {
                  wx.navigateTo({
                    url: '/pages/mine/shop/mycard/detail?id=' + res_1.data.id,
                  })
                }
                else {
                  wx.hideLoading()
                  setTimeout(function () {
                    wx.showToast({
                      title: '店铺未添加会员卡',
                      icon: 'none',
                      duration: 2000
                    })
                  }, 100)
                }
              })
            } else if (resq.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    })
  }
})